<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 任务管理 </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'inspectionmanag:inEnterpriseTask:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.name }}
        </a>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" v-auth="'inspectionmanag:inEnterpriseTask:edit'" type="text">
          <span style="color: #52c41a;font-size:14px">编辑</span>
        </a-button>
        <a-button @click="handleFormDetail({ tid: record.tid })" type="text">
          <span style="color: #4169E1;font-size:14px">详情</span>
        </a-button>
        <Popconfirm
          title="是否确认删除数据"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
          @cancel="handleCancel"
        >
          <a-button v-auth="'inspectionmanag:inEnterpriseTask:edit'" type="text">
            <span style="color: #f5222d;font-size:14px">删除</span>
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormDeatil @register="registerDrawerDetail" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup name="ViewsInspectionmanagInEnterpriseTaskList">
  import { unref, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { inEnterpriseTaskDelete, inEnterpriseTaskListData } from '/@/api/inspectionmanag/inEnterpriseTask';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import { unitRegionListData } from '/@/api/unitmanage/unitRegion';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormDeatil from './formDetail.vue';
  import { officeTreeData } from '/@/api/sys/office';
  import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件

  const { t } = useI18n('inspectionmanag.inEnterpriseTask');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('任务管理表管理'),
  };
  const enterpriseTaskName = ref('');
  const enterpriseTaskParams = computed(() => {
    return {
      name: enterpriseTaskName.value,
    };
  });

  const EnterpriseParams = ref<Recordable>({
    name: ''
  });
  const RegionParams = ref<Recordable>({
    name: ''
  });
  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t(''),
        field: 'name',
        component: 'TreeSelect',
        componentProps: {
          placeholder: t('请输入任务名称'),
          api: inEnterpriseTaskListData,
          resultField: 'list',
          fieldNames: { label: 'name', value: 'name' },
          params: enterpriseTaskParams,
          allowClear: true,
          immediate: false,
          each: false,
          showSelect: true,
          optionFilterProp: 'label',
          showSearch: true,
          onSearch: (value) => {
            enterpriseTaskName.value = value;
            // console.log('输入的值:', value);
            reload()
          },
        },
      },
      {
        label: t(''),
        field: 'taskTypeCode',
        fieldLabel: 'taskTypeName',
        component: 'Select',
        componentProps: {
          maxlength: 40,
          placeholder: t('请输入任务类型'),
          dictType: 'task_type',
          immediate: true,
          allowClear: true,
        },
      },
      {
        label: t(''),
        field: 'enterpriseCode',
        fieldLabel: 'enterpriseName',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('请输入所属主体'),
            api: unitEnterpriseDominantListData,
            params: EnterpriseParams.value,
            fieldNames: { label: 'name', value: 'unitCode' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              EnterpriseParams.value.name = value;
              console.log(EnterpriseParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'regionId',
        fieldLabel: 'regionName',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('请输入任务区域'),
            api: unitRegionListData,
            params: RegionParams.value,
            fieldNames: { label: 'name', value: 'id' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              RegionParams.value.name = value;
              console.log(RegionParams.value);
            },
          };
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('主体名称'),
      dataIndex: 'enterpriseName',
      key: 'a.enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('任务名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('区域名称'),
      dataIndex: 'regionName',
      key: 'a.region_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('任务类型'),
      dataIndex: 'taskTypeName',
      key: 'a.task_type_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('任务要求'),
      dataIndex: 'remark',
      key: 'a.remark',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('任务内容'),
      dataIndex: 'content',
      key: 'a.content',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateTime',
      key: 'a.update_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 180,
      fixed: 'right',
      slots: { customRender: 'action' }, // 使用自定义插槽
    },
  ];

  const [registerTable, { reload }] = useTable({
    api: inEnterpriseTaskListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: null, // 不再使用默认的 actionColumn
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawerDetail, { openDrawer: openDrawerDetail }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  function handleFormDetail(record: Recordable) {
    openDrawerDetail(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await inEnterpriseTaskDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  function handleCancel() {
    // 取消删除操作时的处理逻辑，这里可以不做任何处理，仅为了响应取消事件
    console.log('取消删除操作');
  }
</script>